import Vue from 'vue';
import SchemaConst from './SchemaConst';
import FeatureLoader from '../../framework/FeatureLoader';


export default {
  name: 'SchemaFunc',

  checkBlockByName(schemaName, attachPoint) {
    const schemaBlock = FeatureLoader().schemaMap[schemaName];
    return this.checkBlockAvailable(schemaBlock, attachPoint);
  },

  checkBlockAvailable(schemaBlock, attachPoint) {
    let retValue = true;
    for (const key in schemaBlock.properties) {
      if (Object.hasOwnProperty.call(schemaBlock.properties, key)) {
        if (key.startsWith('com_') && !attachPoint[key]) {
          retValue = false;
          break;
        }
      }
    }
    return retValue;
  },

  fillSubCfg(schemaBlock, attachPoint) {
    for (const key in schemaBlock.properties) {
      if (Object.hasOwnProperty.call(schemaBlock.properties, key)) {
        if (key.startsWith('com_') && !attachPoint[key]) {
          const element = schemaBlock.properties[key];
          const schemaId = element.$ref.split('.')[0];
          const tmpCfg = SchemaConst.getDemoCfg(schemaId);
          Vue.set(attachPoint, key, tmpCfg);
        }
      }
    }
  }
};